package com.leehom.arch.base.reportquery;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.leehom.arch.base.reportquery.dynsql.DynSql;
import com.leehom.arch.base.reportquery.utils.ParamTypeCaster;
import com.leehom.arch.base.reportquery.utils.SortParamMerger;
import com.leehom.arch.common.Page;
import com.leehom.arch.common.PageSearchBean;
import com.leehom.arch.common.SortBean;
import com.leehom.arch.common.result.ListResult;
import com.leehom.arch.common.result.PageListResult;

/**
 * @类名: ReportQueryWrapper
 * @说明: 报表查询包装器，参数类型转换
 *
 * @author   leehom
 * @Date	 2020年12月4日 下午1:46:49
 * 修改记录：
 *   > 增加排序参数
 *   
 * @see 
 */
@Component	
public class ReportQueryWrapper {
	
	/** 排序参数前缀*/
	public static final String SORT_PARAM_PRE = "_order_";

	@Autowired
	private ReportQuery reportQuery;
	
	public int execSql(String sqlName, Map<String, String> params) {
    	//
    	DynSql dynSql = reportQuery.dynSql(sqlName);
    	// 查询参数类型转换
		Map<String, Object> castdParam;
		if(params!=null)
			castdParam = ParamTypeCaster.cast(params, dynSql.getParamTypeMap());
		else
			castdParam = new HashMap<String, Object>();
		// 执行sql
		return reportQuery.execSql(sqlName, castdParam);

    }
	
	public int execJpql(String jpqlName, Map<String, String> params) {
    	//
    	DynSql dynSql = reportQuery.dynSql(jpqlName);
    	// 参数类型转换
		Map<String, Object> castdParam;
		if(params!=null)
			castdParam = ParamTypeCaster.cast(params, dynSql.getParamTypeMap());
		else
			castdParam = new HashMap<String, Object>();
		// 执行sql
		return reportQuery.execJpql(jpqlName, castdParam);

    }
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public ListResult queryNamedSQL(String sqlName, Map<String, String> params) {
    	//
    	DynSql dynSql = reportQuery.dynSql(sqlName);
    	// 参数类型转换
		Map<String, Object> castdParam;
		if(params!=null)
			castdParam = ParamTypeCaster.cast(params, dynSql.getParamTypeMap());
		else
			castdParam = new HashMap<String, Object>();
		// 执行sql
		List r = (List)reportQuery.queryNamedSQL(sqlName, castdParam);
		return new ListResult(r);

    }
	
    public PageListResult pageSearchBySql(String sqlName, PageSearchBean searchBean) {
    	//
    	DynSql dynSql = reportQuery.dynSql(sqlName);
    	// 查询参数类型转换
		Map<String, Object> castdParam;
		if(searchBean.getParams()!=null)
			castdParam = ParamTypeCaster.cast(searchBean.getParams(), dynSql.getParamTypeMap());
		else
			castdParam = new HashMap<String, Object>();
		// 排序参数
		SortBean sortBean = searchBean.getSortBean();
		if(sortBean!=null){
			SortParamMerger.merge(castdParam, sortBean);
		}
		// 分页
		Page p = new Page();
		p.setPage(searchBean.getPage());
		p.setPageSize(searchBean.getPageSize());
		// 执行查找
	    p = reportQuery.pageQueryNamedSQL(p, sqlName, castdParam);
		return new PageListResult(p, p.getRows());
    }
    
    public PageListResult pageSearchByJpql(String jpqlName, PageSearchBean searchBean) {
    	//
    	DynSql dynSql = reportQuery.dynSql(jpqlName);
    	// 参数类型转换
		Map<String, Object> castdParam;
		if(searchBean.getParams()!=null)
			castdParam = ParamTypeCaster.cast(searchBean.getParams(), dynSql.getParamTypeMap());
		else
			castdParam = new HashMap<String, Object>();
		// 排序参数
		SortBean sortBean = searchBean.getSortBean();
		if(sortBean!=null){
			SortParamMerger.merge(castdParam, sortBean);
		}
		// 分页
		Page p = new Page();
		p.setPage(searchBean.getPage());
		p.setPageSize(searchBean.getPageSize());
		// 执行查找
	    p = reportQuery.pageQueryNamedHQL(p, jpqlName, castdParam);
		return new PageListResult(p, p.getRows());
    }
    
}